#!/usr/bin/env python

# unzip a file encoded in GBK to unicode

import zipfile,os,sys

if len(sys.argv) == 2:
    fname = sys.argv[1]
else:
    print "Usage: python %s filename\n\n"%sys.argv[0]
    sys.exit()

if (not os.path.exists(fname)) or (not zipfile.is_zipfile(fname)):
    print fname, " is not a zipfile or not exists"
    sys.exit()

print "Strting ...\n"
f=zipfile.ZipFile(fname)
for finfo in f.infolist():
    savename=unicode(finfo.filename, 'GBK')
    print "Infalting " + savename

    if os.path.exists(savename):
    	continue

    if finfo.file_size == 0:
        #print "dir: %s"%savename
        os.makedirs(savename)
    else:
        #print "file: %s"%savename
        dirn = savename.rpartition('/')[0]
        if "/" in savename:
            if not os.path.exists(dirn):
                os.makedirs(dirn)
        of = open(savename, 'w')
        of.write(f.read(finfo.filename))
        of.close()

f.close()
print "Done ;-)"
